﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Kingdee.BOS.WebApi.Client;

namespace FastAPI.WebApiHelper
{
    public class InvokeHelper
    {
        static InvokeHelper()
        {
            var builder = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json");

            IConfigurationRoot config = builder.Build();
            cloudUrl = config["KingdeeCloud:K3CloudUrl"];
            acctID = config["KingdeeCloud:dbId"];
            userName = config["KingdeeCloud:username"];
            password = config["KingdeeCloud:password"];
            lcID = config["KingdeeCloud:Lcid"];
        }


        /// <summary>
        /// 星空 业务站点地址
        /// </summary>
        public static string cloudUrl = null;
        /// <summary>
        /// 账套ID
        /// </summary>
        public static string acctID = null;
        /// <summary>
        /// 第三方系统登录授权的 集成用户名称
        /// </summary>
        public static string userName = null;
        /// <summary>
        /// 第三方系统登录授权的 集成用户密码
        /// </summary>
        public static string password = null;
        /// <summary>
        /// 账套语系
        /// </summary>
        public static string lcID = null;

        
        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        public static string Login()
        {
            HttpClient httpClient = new HttpClient();

            httpClient.Url = string.Concat(cloudUrl,
            "Kingdee.BOS.WebApi.ServicesStub.AuthService.ValidateUser.common.kdsvc");

            List<object> Parameters = new List<object>();



            Parameters.Add(acctID);//账套
            Parameters.Add(userName);//用户名
            Parameters.Add(password);//密码
            Parameters.Add(lcID);//2052代表中文  1033代表英文 1049代表俄语
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }

        /// <summary>
        /// 保存
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Save(string formId, string content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Save.common.kdsvc");

            List<object> Parameters = new List<object>();
            //业务对象Id 
            Parameters.Add(formId);
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }


        /// <summary>
        /// 提交
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Submit(string formId, string content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Submit.common.kdsvc");

            List<object> Parameters = new List<object>();
            //业务对象Id 
            Parameters.Add(formId);
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }


        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Audit(string formId, string content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Audit.common.kdsvc");

            List<object> Parameters = new List<object>();
            //业务对象Id 
            Parameters.Add(formId);
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }

        /// <summary>
        /// 反审核
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string UnAudit(string formId, string content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.UnAudit.common.kdsvc");

            List<object> Parameters = new List<object>();
            //业务对象Id 
            Parameters.Add(formId);
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }


        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string CancelAssign(string formId, string content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.CancelAssign.common.kdsvc");

            List<object> Parameters = new List<object>();
            //业务对象Id 
            Parameters.Add(formId);
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }


        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string Delete(string formId, string content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.Delete.common.kdsvc");

            List<object> Parameters = new List<object>();
            //业务对象Id 
            Parameters.Add(formId);
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }


        /// <summary>
        /// 单据查询
        /// </summary>
        /// <param name="content"></param>
        /// <returns></returns>
        public static string ExecuteBillQuery(ExecuteBillQueryParam content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExecuteBillQuery.common.kdsvc");

            List<object> Parameters = new List<object>();
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }

        /// <summary>
        /// 作废
        /// </summary>
        /// <param name="formId"></param>
        /// <param name="content"></param>
        /// <param name="opNumber">Forbid 禁用，Enable反禁用</param>
        /// <returns></returns>
        public static string ExcuteOperation(string formId, string opNumber, string content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.ExcuteOperation.common.kdsvc");

            List<object> Parameters = new List<object>();
            //业务对象Id 
            Parameters.Add(formId);
            //操作码
            Parameters.Add(opNumber);
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();
        }


        /// <summary>
        /// 查询库存接口
        /// </summary>
        /// <param name="pushData"></param>
        /// <returns></returns>
        public static string GetSysReportData(string formId, string content)
        {
            HttpClient httpClient = new HttpClient();
            httpClient.Url = string.Concat(cloudUrl, "Kingdee.BOS.WebApi.ServicesStub.DynamicFormService.GetSysReportData.common.kdsvc");//Kingdee.K3.SCM.WebApi.ServicesStub.ExpectQtyQueryWebApi.GetExpectQty

            List<object> Parameters = new List<object>();
            //业务对象Id 
            Parameters.Add(formId);
            //Json字串
            Parameters.Add(content);
            httpClient.Content = JsonConvert.SerializeObject(Parameters);
            return httpClient.SysncRequest();

        }
    }
}
